package openbang.account.web.portal.client;

import openbang.account.service.entity.UserAccount;
import openbang.account.service.intf.AccountService;
import openbang.account.service.vo.AccountServiceEnum;
import openbang.account.service.vo.UserLoginInfo;
import openbang.account.web.portal.widget.EmailTextBox;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;


/**
 * 注册控制 
 * @author aibo zeng
 *
 */
public class Register implements EntryPoint {
	private EmailTextBox email = null; 
	private PasswordTextBox pwd1 = null;
	private PasswordTextBox pwd2 = null;
	private TextBox userName = null;
	private TextBox niceName = null;
	private Button  register = null;
	private HTML emailTitleLabel = new HTML("系统自动检测是否被占用");
	private HTML pwd1Label = new HTML();
	private HTML pwd2Label = new HTML();

	private Button  googleLogin = null;
	
	//录入的邮箱是否可以注册
	private boolean emailValid = false;
	
	private void bindUI(){
		register = Button.wrap(Document.get().getElementById("regBtn"));
		email = EmailTextBox.wrap(Document.get().getElementById("emailTxt"));
		pwd1   = PasswordTextBox.wrap(Document.get().getElementById("pwd1Txt"));
		pwd2   = PasswordTextBox.wrap(Document.get().getElementById("pwd2Txt"));
		userName = TextBox.wrap(Document.get().getElementById("userNameTxt"));
		niceName = TextBox.wrap(Document.get().getElementById("niceNameTxt"));
		RootPanel.get("emailTitle").add(emailTitleLabel);
		RootPanel.get("pwd1Title").add(pwd1Label);
		RootPanel.get("pwd2Title").add(pwd2Label);
		googleLogin = Button.wrap(Document.get().getElementById("toGoogleLoginBtn"));
	}	
	@Override
	public void onModuleLoad() {
		bindUI();

		email.addBlurHandler(new BlurHandler() {
			public void onBlur(BlurEvent event) {
				//焦点离开，检查email是否已经注册
				doFindEmail();
			}
		});
		pwd2.addBlurHandler(new BlurHandler() {
			public void onBlur(BlurEvent event) {
				//焦点离开，检查两次密码是否一致
				doCheckPwd();
			}
		});
		register.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				doRegister();
			}
		});
		//加载完，注册按钮不管用，录入信息过程中才管用
		register.setEnabled(false);
		
		googleLogin.setEnabled(true);
		googleLogin.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				//获取 Google登录的链接
				AccountService.Util.getInstance().getLoginUserInfo(getLoginAfterUrl(),new AsyncCallback<UserLoginInfo>(){
					public void onFailure(Throwable caught) {
						Window.alert(caught.getMessage());
					}
					public void onSuccess(UserLoginInfo result) {
						toSwitchLoginAfterUrl(result.getLoginUrl());
					}
				});				
			}
		});		
		
	}
	
	public void doFindEmail(){
		if(email.invalidNum()>0){
			emailTitleLabel.setHTML("<font color=\"red\">输入的邮箱地址不合法</font>");
			return;
		}
			//后台检查
			AccountService.Util.getInstance().findEmail(email.getArrValue()[0], new AsyncCallback<Integer>(){
				public void onSuccess(Integer result) {
					//Window.alert(result);
					if(result==AccountServiceEnum.OK.getCode()){
						//找到
						emailValid = false;
						register.setEnabled(false);
						emailTitleLabel.setHTML("<font color=\"red\">已被占用</font>");
					}
					else{
						register.setEnabled(true);
						emailValid = true;
						emailTitleLabel.setHTML("<font color=\"blue\">可以注册</font>");
					}
				}
				public void onFailure(Throwable caught) {
					Window.alert(caught.getMessage());
				}				
			});		
	}
	
	UserAccount account = new UserAccount();
	private void bindData(){
		if(email.getText()==null){
			email.setText("");
		}
		if(pwd1.getText()==null){
			pwd1.setText("");
		}
		if(niceName.getText()==null){
			niceName.setText("");
		}
		if(userName.getText()==null){
			userName.setText("");
		}
		account.setEmail(email.getArrValue()[0]);
		account.setPwd(pwd1.getText().trim());
		account.setNiceName(niceName.getText().trim());
		account.setUserName(userName.getText().trim());		
	}
	
	public void doRegister(){
		bindData();
		doCheckPwd();
		if(!register.isEnabled()){
			return;
		}
		doCheckName();
		if(!register.isEnabled()){
			return;
		}
		if(!emailValid){
			Window.alert("邮箱不可用,请重新录入");
			return;
		}
		register.setEnabled(false);
		AccountService.Util.getInstance().registerAccount(account, new AsyncCallback<Integer>(){
			public void onSuccess(Integer result) {
				if(result==AccountServiceEnum.OK.getCode()){
					Window.alert("注册成功 email="+account.getEmail()+",您可以登录系统了.");
					//没有设置自动登录 toSwitchLoginAfterUrl(null);
					email.setText("");
					pwd2.setText("");
					pwd1.setText("");
					niceName.setText("");
					userName.setText("");
				}
				else{
					emailTitleLabel.setHTML("<font color=\"blue\">注册失败:"+AccountServiceEnum.getDesc(result)+"</font>");
				}
				register.setEnabled(true);
			}
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
				register.setEnabled(true);
			}				
		});
	}
	
	public void doCheckPwd(){
		if(pwd1.getText()==null||pwd1.getText().length()<=0){
			Window.alert("密码不可为空");
			register.setEnabled(false);
			pwd1.setFocus(true);
			return;
		}
		if(!pwd1.getText().equals(pwd2.getText())){
			pwd2Label.setHTML("<font color=\"red\">两次密码不一致</font>");
			register.setEnabled(false);
			//Window.alert("两次密码不一致");
		}
		else{
			pwd2Label.setHTML("<font color=\"blue\">两次密码一致</font>");
			register.setEnabled(true);
		}
	}

	public void doCheckName(){
		if(userName.getText()==null || userName.getText().trim().length()<=0){
			Window.alert("姓名不可为空");
			register.setEnabled(false);
			userName.setFocus(true);
			return;
		}
		if(niceName.getText()==null || niceName.getText().trim().length()<=0){
			Window.alert("昵称不可为空");
			register.setEnabled(false);
			niceName.setFocus(true);
			return;
		}
		register.setEnabled(true);
	}
	
	//获取登录后应该跳转的页面  在 JSP的 javascript中定义      google登录需要
	public static native String getLoginAfterUrl() /*-{  
         return parent.getLoginAfterDefaultUrl();
    }-*/;	
	//调用登录后应该跳转的页面  参数url可以为空
	public static native void toSwitchLoginAfterUrl(String url) /*-{  
         parent.toLoginAfterUrl(url);
    }-*/;		
}
